package com.auto.survey.activity.config

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.auto.survey.R
import com.auto.survey.activity.config.entity.ConfigBean
import com.auto.survey.base.ICreatView
import com.auto.survey.widget.CentreToast
import com.google.gson.Gson
import kotlinx.coroutines.launch

/**
 * 测站配置
 */
class ConfigSurveyStationView(config_rlt: ConfigBean, val viewModel: ConfigViewModle, val activity: AppCompatActivity) :
    ICreatView {

    private var mConfigRlt = config_rlt;

    companion object {
        private val TAG:String = ConfigSurveyStationView::class.java.simpleName
    }

    private val mActivity = activity
    private lateinit var mView: View
    private val editList = mutableListOf<EditText>()

    override fun creatView(context: Context, parent:ViewGroup) {
        mView = LayoutInflater.from(context).inflate(R.layout.view_serveystation_config, parent, false)

        //湿度
        val edit_rh = mView.findViewById<EditText>(R.id.edit_surveystation_sd)
        edit_rh.addTextChangedListener(EditTextWater(edit_rh))
        editList.add(edit_rh)

        //温度
        val edit_tp = mView.findViewById<EditText>(R.id.edit_surveystation_wd)
        edit_tp.addTextChangedListener(EditTextWater(edit_tp))
        editList.add(edit_tp)

        //棱镜常数
        val edit_pc = mView.findViewById<EditText>(R.id.edit_surveystation_ljcs)
        edit_pc.addTextChangedListener(EditTextWater(edit_pc))
        editList.add(edit_pc)

        //气压
        val edit_ap = mView.findViewById<EditText>(R.id.edit_surveystation_qy)
        edit_ap.addTextChangedListener(EditTextWater(edit_ap))
        editList.add(edit_ap)

        //仪器高度
        val edit_ih = mView.findViewById<EditText>(R.id.edit_surveystation_yqhd)
        edit_ih.addTextChangedListener(EditTextWater(edit_ih))
        editList.add(edit_ih)

        //测回次数
        val edit_nos = mView.findViewById<EditText>(R.id.edit_surveystation_chcs)
        edit_nos.addTextChangedListener(EditTextWater(edit_nos))
        editList.add(edit_nos)

        //监听viewModel的LaveData的数据变化，发生变化则触发下面的代码
        //初始时候从数据库取出历史数据，初始化各个edittext
        mActivity.lifecycleScope.launch {
            viewModel.bean.observe(mActivity) { it->
                Log.e(TAG,"数据库查询测站配置==> : ${Gson().toJson(it?.config)}")
                if (it?.config != null) {
                    //查询到数据库中的数据后，要同步外部传进来的初始化的数据
                    mConfigRlt = it.config
                    Log.e(TAG,"同步数据测站配置==> : ${Gson().toJson(mConfigRlt)}")
                }

                edit_rh.setText("${it?.config?.hum_data}")
                edit_tp.setText("${it?.config?.tmp_data}")
                edit_pc.setText("${it?.config?.pc_data}")
                edit_ap.setText("${it?.config?.kap_data}")
                edit_ih.setText("${it?.config?.higt_data}")
                edit_nos.setText("${it?.config?.stnum_data}")
            }
        }

    }

    override fun checkView(): Boolean {
        editList.forEach { editText ->
            if (editText.id == R.id.edit_surveystation_yqhd && TextUtils.isEmpty(editText.text)) {
                CentreToast.instance.show("请输入仪器高度", false)
                return false
            }

            if (editText.id == R.id.edit_surveystation_chcs && TextUtils.isEmpty(editText.text)) {
                CentreToast.instance.show("请输入测回次数", false)
                return false
            }

            if (editText.id == R.id.edit_surveystation_wd && !TextUtils.isEmpty(editText.text)) {
                val tep = editText.text.toString().toDoubleOrNull()?:0.0
                if(tep < -273.15){CentreToast.instance.show("温度不能低于绝对0度", false)}
                return tep >= -273.15
            }
        }

        viewModel.updateConfigDataStation(mConfigRlt)
        return true
    }

    override fun getView(): View {
        return mView
    }

    override fun getTitle(): String {
        return  "测站配置"
    }

    inner class EditTextWater(val editText: EditText) : TextWatcher {
        override fun afterTextChanged(s: Editable?) {

            when(editText.id){
                R.id.edit_surveystation_sd -> mConfigRlt.hum_data = s?.toString()?.toDoubleOrNull()?:0.0//湿度
                R.id.edit_surveystation_wd-> mConfigRlt.tmp_data = s?.toString()?.toDoubleOrNull()?:0.0//温度
                R.id.edit_surveystation_ljcs-> mConfigRlt.pc_data = s?.toString()?.toDoubleOrNull()?:0.0//棱镜常数
                R.id.edit_surveystation_qy-> mConfigRlt.kap_data = s?.toString()?.toDoubleOrNull()?:0.0//气压
                R.id.edit_surveystation_yqhd-> mConfigRlt.higt_data = s?.toString()?.toDoubleOrNull()?:0.0//仪器高度
                R.id.edit_surveystation_chcs-> mConfigRlt.stnum_data = s?.toString()?.toIntOrNull()?:1//测回次数
            }


        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }


    }




}